package com.mbl.base.modular.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mbl.base.core.common.dto.MblResult;
import com.mbl.base.core.common.exception.BaseException;
import com.mbl.base.core.utils.UtilHttp;
import com.mbl.base.core.utils.UtilSecurity;
import com.mbl.base.modular.mall.entity.IntegralGoodUser;
import com.mbl.base.modular.mall.entity.IntegralGoods;
import com.mbl.base.modular.mall.service.IIntegralGoodsService;
import com.mbl.base.modular.member.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mbl.base.core.common.controller.BaseController;


/**
 * <p>
 * 积分商品表 前端控制器
 * </p>
 *
 * @author lwj
 * @since 2020-04-23
 */
@RestController
@RequestMapping("/mall/integral-goods")
public class IntegralGoodsController extends BaseController<IntegralGoods, IIntegralGoodsService> {

    @PostMapping("exchange")
    public Object exchange(String id) {
        final User doUser = UtilSecurity.getDoUser();
        final IntegralGoods integralGoods = getService().getById(id);
        if(doUser.getIntegral() < integralGoods.getIntegral()){
            throw new BaseException("积分不足");
        }
        final IntegralGoodUser integralGoodUser = new IntegralGoodUser();
        integralGoodUser.setIntegralGoodId(integralGoods.getId());
        integralGoodUser.setUserId(doUser.getId());
        integralGoodUser.setIntegralGoodName(integralGoods.getName());
        integralGoodUser.insert();
        doUser.setIntegral(doUser.getIntegral() - integralGoods.getIntegral()).updateById();
        return MblResult.success();
    }

    @Override
    public void machiningList(QueryWrapper<IntegralGoods> queryWrapper, String condition) {
        final String name = UtilHttp.getPara("name");
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        super.machiningList(queryWrapper, condition);
    }

}

